package com.bootdo.acitvity.DDD.domain.entity;

import lombok.Getter;

public class ChannelFrozenStock extends Stock {

    /** 批次id */
    @Getter
    private String fbatchId;

    /** 渠道id */
    @Getter
    private Long fchannelId;

    @Getter
    private String sessionId;

    @Getter
    private TYPE type;

    public enum TYPE {
        FROM_LOCKED_STOCK,
        FROM_SHARED_STOCK
    }

    private ChannelFrozenStock(Builder builder) {
        super(builder.stockAmount);
        this.fchannelId=builder.fchannelId;
        this.type=builder.type;
        this.fbatchId=builder.fbatchId;
        this.sessionId=builder.sessionId;
    }

    public static class Builder {
        private String fbatchId;
        private Long stockAmount;
        private Long fchannelId;
        private TYPE type;
        private String sessionId;


        public Builder(){}
        public Builder(ChannelFrozenStock frozenStock){
            stockAmount = frozenStock.getStockAmount();
            fchannelId = frozenStock.fchannelId;
            type = frozenStock.getType();
            fbatchId = frozenStock.getFbatchId();
        }
        public Builder setType(TYPE type){
            this.type = type;
            return this;
        }
        public Builder fromSharedStock(){
            type = TYPE.FROM_SHARED_STOCK;
            return this;
        }
        public Builder fromLockedStock(){
            type = TYPE.FROM_LOCKED_STOCK;
            return this;
        }
        public Builder setStockAmount(Long stockAmount){
            this.stockAmount = stockAmount;
            return this;
        }
        public Builder setStockAmount(Stock stock){
            this.stockAmount = stock.getStockAmount();
            return this;
        }
        public Builder setChannelId(Long fchannelId){
            this.fchannelId = fchannelId;
            return this;
        }

        public Builder setSessionId(String sessionId){
            this.sessionId = sessionId;
            return this;
        }

        public Builder setFBatchId(String fbatchId){
            this.fbatchId = fbatchId;
            return this;
        }
        public ChannelFrozenStock build(){
            return new ChannelFrozenStock(this);
        }
    }

    public static Builder getBuilder(){
        return new Builder();
    }
    public static Builder getBuilder(ChannelFrozenStock lockedStock){
        return new Builder(lockedStock);
    }

    /**
     * 增加锁定库存
     * @param stock
     * @return
     */
    @Override
    public ChannelFrozenStock add(Stock stock){
        return getBuilder(this).setStockAmount(this.getStockAmount() + stock.getStockAmount()).build();
    }

    /**
     * 扣减锁定库存
     * @param stock
     * @return
     */
    @Override
    public ChannelFrozenStock reduce(Stock stock){
        return getBuilder(this).setStockAmount(this.getStockAmount() - stock.getStockAmount()).build();
    }

}
